import sys
import math

import networkx

from MathFunc import *


def ppmethod(profile1, profile2, ):



def pp(id_set, profile, N, out_file):
    G = networkx.XGraph()
    wfile = open(out_file, 'w')
    id_list = list(id_set)
    for i in xrange(0, len(id_list)-1):
        id1 = id_list[i]
        if id1 not in profile:
            continue

        profile1 = set(profile[id1].keys())
        m = len(profile1)

        if m < N*0.1:
            continue

        for j in xrange(i+1, len(id_list)):
            id2 = id_list[j]
            if id2 not in profile:
                continue
            profile2 = set(profile[id2].keys())


            n = len(profile2)

            if n < N*0.1:
                continue

            k = len(profile1.intersection(profile2))

            if m == 0 or n == 0 or k == 0:
                continue

            p = 1-cumu_hyper(k, N, m, n)
            line = '\t'.join([id1,id2,str(p)])+'\n'
            G.add_nodes_from([id1, id2])
            G.add_edge(id1, id2, p)
            #print line
            wfile.write('\t'.join([id1,id2,str(p),'pp'])+'\n')
    wfile.close()
    return G


def gn(id_set, taxon_list, profile, id2location, id2taxon, out_file):
    N = len(id_set)
    wfile = open(out_file, 'w')
    id_list = list(id_set)
    for i in xrange(0, len(id_list)-1):
        id1 = id_list[i]
        if id1 not in profile:
            continue
        for j in xrange(i+1, len(id_list)):
            id2 = id_list[j]
            if id2 not in profile:
                continue
            p1 = profile[id1]
            p2 = profile[id2]

            same_taxon = set(p1.keys()).intersection(p2.keys())

            if len(same_taxon) == 0:
                continue

            d = math.fabs(id2location[id1] - id2location[id2])
            if d > (N-1)/2:
                d = N-1-d
            if d == 0:  # some CDS may starts from the same point
                d = 1
            m = 1
            p = 2.0*d/(N-1)

            for taxon in same_taxon:
                tmp_id1 = p1[taxon]
                tmp_id2 = p2[taxon]
                t1 = id2taxon[tmp_id1]
                t2 = id2taxon[tmp_id2]

                if t1 == t2:
                    loc1 = id2location[tmp_id1]
                    loc2 = id2location[tmp_id2]
                    m += 1
                    d = math.fabs(loc1 - loc2)
                    N = len(taxon_list[t1[0]][t1[1]][t1[2]])
                    if d > (self.N-1)/2:
                        d = self.N-1-d
                    if d == 0:
                        d = 1
                    p *= 2.0*d/(N-1)

            if m == 1:
                continue

            pm = 0

            if p == 0:
                return 0

            for i in range(0,m-1):
                y = -1*math.log(p)
                pm += _possion(i, y)

            wfile.write('\t'.join([id1,id2,str(p),'gn'])+'\n')





if __name__ == '__main__':
    '''
    seqdb = SeqDB('sqlite://')
    seqdb.create()
    print 'start reading file'
    seqdb.read_file("gene.loc.nosort.test", "gcn")
    print 'gcn file read'
    seqdb.read_file("orth_pair", "orth_pair")
    print 'orth_pair file read'
    Method(seqdb, 2).phyl_taxon()
    raw_input()
    '''
    # just test
    id2start, taxon_dict, id2taxon = read_gcn_file('gene.loc.nosort')
    print 'partially done'
    find_location(id2start, taxon_dict)
    del id2start
    profile, id_set = build_profile('orth121', '180454', taxon_dict, id2taxon)
    N = len(taxon_dict)
    #del id2taxon
    pp(id_set, profile, N, 'test.pp')


#END